package com.servlet;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.huojiaDao;

/**
 * Servlet implementation class Add
 */
@WebServlet("/Add")
public class Add extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Add() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");//防止出现中文乱码
		
		// 获取注册信息
		String commodity =request.getParameter("A");
		String number =request.getParameter("B");
		String unit_price =request.getParameter("C");
		

		
		if (commodity.isEmpty() || number.isEmpty() || unit_price.isEmpty()) {
		    // 如果commodity、number、unit_price任一项为空，则执行失败逻辑
		    request.getSession().setAttribute("A1", "添加失败 请填写完整信息");
		    response.sendRedirect("/aaa/index.jsp");
		} else {
		    boolean a = huojiaDao.add(commodity, number, unit_price);
		    if (a) {
		        request.getSession().setAttribute("A1", "添加成功");
		    } else {
		        request.getSession().setAttribute("A1", "添加失败 已存在同名商品");
		    }
		    response.sendRedirect("/aaa/index.jsp");
		}	
		
	}

}
